import 'package:flutter/material.dart';
import 'package:flutter_learn/gen/assets.gen.dart';
import 'package:flutter_svg/svg.dart';
import 'package:provider/provider.dart';

import 'model/bottom_tab_model.dart';
import 'utils/asset_util.dart';
import 'utils/main_tab_config.dart';
import 'utils/provider_util.dart';

class HomeTab extends StatefulWidget {
  const HomeTab({super.key});

  @override
  State<HomeTab> createState() => _HomeTabState();
}

class _HomeTabState extends State<HomeTab> {
  List<BottomTabModel> bottomTabLIst = [];
  int _currentIndex = 0;
  
  @override
  void initState() {
    super.initState();
    bottomTabLIst.add(BottomTabModel('首页',Assets.tab.homeUnselect,Assets.tab.homeSelect));
    bottomTabLIst.add(BottomTabModel('发现',Assets.tab.findUnselect,Assets.tab.findSelect));
    bottomTabLIst.add(BottomTabModel('购物车',Assets.tab.groupUnselect,Assets.tab.groupSelect));
    bottomTabLIst.add(BottomTabModel('我的',Assets.tab.mineUnselect,Assets.tab.mineSelect));
  }
  
  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
          title: Title(
        color: Colors.red,
        child: Text('flutter 学习'),
      )),
      body: IndexedStack(
        index: _currentIndex,
        children: MainTabConfig.getMainTabConfig(),
      ),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _currentIndex,
        type: BottomNavigationBarType.fixed,
        items: buildBottomTab(),
        onTap: (index){
          setState(() {
            _currentIndex = index;

          });
        },
      ),
    );
  }

  List<BottomNavigationBarItem> buildBottomTab(){
    return List.generate(bottomTabLIst.length, (index){
      return BottomNavigationBarItem(
        label: bottomTabLIst[index].label,
        icon: AssetUtil.svgImage(bottomTabLIst[index].icon,size: const Size.square(30)),
        activeIcon: AssetUtil.svgImage(bottomTabLIst[index].activeIcon,size: const Size.square(30)),);

    });
  }
}
